<?php
/**
 * @ClassName: Merchant
 * @Description: 商户列表
 * @author: gydtrade
 * @Date: 2024-09-10 22:43
 * @Version: V1.0.0
 */

namespace app\admin\controller\merchant;

use app\common\enum\CommonEnum;
use app\common\enum\MerchantEnum;
use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Merchant extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\Merchant();
        $this->pageTitle = '商户列表';
        $this->enableField = 'status';
        $this->pagesize = 10;

        $this->indexWith = ['member'];

        $this->selectTextField = '{id}#{merchant_short_name}';
        $this->selectFields = 'id,merchant_short_name';
        $this->selectSearch = 'merchant_short_name';
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;
        $search->text('name', '名称');
        $search->select('user_type', '商户归属')->options(MerchantEnum::storeTypeText(true));
        $search->select('member_id', '管理员')->dataUrl(url('/admin/member/selectPage'));
        $search->select('merchant_type', '商户类型')->options(CommonEnum::merchantTypes(true));
        $search->select('status', '状态')->options([1 => '正常', 3 => '锁定', -1 => '已禁用']);
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (!empty($searchData['name'])) {
            $where[] = ['merchant_short_name|store_name|username|contact_name|contact_phone', 'like', '%' . $searchData['name'] . '%'];
        }
        if (isset($searchData['user_type']) && $searchData['user_type'] != '') {
            $where[] = ['user_type', 'eq', $searchData['user_type']];
        }
        if (isset($searchData['member_id']) && $searchData['member_id'] != '') {
            $where[] = ['member_id', 'eq', $searchData['member_id']];
        }
        if (isset($searchData['merchant_type']) && $searchData['merchant_type'] != '') {
            $where[] = ['merchant_type', 'eq', $searchData['merchant_type']];
        }
        if (isset($searchData['status']) && $searchData['status'] != '') {
            $where[] = ['status', 'eq', $searchData['status']];
        } else {
            $where[] = ['status', 'in', [1, 3, -1]];
        }
        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->match('user_type', '商户归属')->options(MerchantEnum::storeTypeText(true))->mapClassGroup([[1, 'info'], [2, 'success']]);
        $table->match('merchant_type', '商户类型')->options(CommonEnum::merchantTypes(true));
        $table->show('merchant_short_name', '商户简称');
        $table->fields('contact_info', '商户联系信息')->with(
            $table->show('contact_name', '联系人'),
            $table->show('contact_phone', '联系电话'),
            $table->show('email', '联系邮箱')
        );
        $table->show('cust_id', '商户号');
        $table->match('status', '状态')->options([0 => '待审核', 1 => '已通过', 2 => '已驳回'])->mapClassGroup([[0, 'info'], [1, 'success'], [2, 'danger']]);

        $table->fields('review_info', '审核信息')->with(
            $table->show('review_time', '审核时间'),
            $table->show('review_opinion', '审核意见')
        );

        $table->fields('times', '申请/修改时间')->with(
            $table->show('create_time', '申请时间'),
            $table->show('update_time', '修改时间')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, review_time, create_time');

        $table->sortable('id, create_time');

        $table->getToolbar()
            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
            ->btnDelete()
            ->btnView();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->only([], 'post');

        $result = $this->validate($data, []);

        if (!$result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }

}